<?php

/**
 * --------------------------------
 * Default SubjectController
 *
 * 课程管理的统一类  SubjectController
 * 
 * --------------------------------
 */

class SubjectController extends BaseController{

 // /**
 //     * 构造函数
 //     * 统一用户Session验证
 //     * 
 //     */
 //    public function __construct(){
 //    	$check_result = Basecontroller::session('internal');
 //    	if(!$check_result) 
 //    		exit();
 //    }
	/**
	 * 查询所有课程信息接口 
	 * @param
	 * @return Response
	 */
	public function index(){
     
		$subject = DB::table('subject')
		                    ->select('id','name')
		                    ->where('is_del',0)
		                    ->get();                 
		return Response::json(['error'=>false,'result'=>$subject],200);
    }


    /**
	 * 新增科目接口 
	 * @param 
	 * @return Response
	 */
	public function store(){

		if(!Input::has('name')){
			return Response::json(['error'=>true,'message'=>'缺少重要参数'],201);
		}
		$name = Input::get('name');
		$exist_subject = DB::table('subject')->select('name')->get();
		foreach ($exist_subject as $key => $value) {
			if($name == $value->name){
				return Response::json(['error'=>true,'message'=>'课程已存在'],201);
			}
		}
		$id = DB::table('subject')->insertGetId(array('name'=>$name));
	 	if($id){
	 		return Response::json(['error'=>false,'message'=>'创建成功','result'=>$id],200);	
	 	}else{
	 		return Response::json(['error'=>true,'message'=>'创建失败'],201);
	 	}
		 	
	}
}	